package mole.user;

import Cabbage.MySQLServerDAL.*;

public class userx
{


	public static boolean test = false;
	public static boolean isadminlogin = false;
	public static boolean a1 = false;
	public static boolean a2 = false;
	public static boolean a3 = false;

	public static boolean b1 = false;
	public static boolean b2 = false;
	public static boolean b3 = false;
	public final boolean login(String username, String pwd)
	{
		if (username.equals("admin"))

		{
			return adminlogin(username, pwd);
		}
		else
		{
			DataSet ds = DbHelperMySQL.Query("select * from customers_copy where username='" + username + "'");
			DataTable dt = ds.Tables[0]; //     Tables

			if (dt.Rows.size() == 0)
			{
				return false;
			}
			String p = dt.Rows[0]["password"].toString();
			if (pwd.equals(p))
			{

					isadminlogin = false;
					a1 = Boolean.parseBoolean(dt.Rows[0]["a1"].toString());
					a2 = Boolean.parseBoolean(dt.Rows[0]["a2"].toString());
					b1 = Boolean.parseBoolean(dt.Rows[0]["b1"].toString());
					b2 = Boolean.parseBoolean(dt.Rows[0]["b2"].toString());
					return true;

			}
			else

			{
				return false;
			}


		}




	}

	public final boolean adminlogin(String username, String pwd)
	{
		DataSet ds = DbHelperMySQL.Query("select * from customers where username='" + username + "'");
		DataTable dt = ds.Tables[0]; //     Tables

		if (dt.Rows.size() == 0)
		{
			return false;
		}
		String p = dt.Rows[0]["pwd4net"].toString();
		if (pwd.equals(p))
		{
			if (username.equals("admin"))
			{
				isadminlogin = true;
			}
			else
			{
				isadminlogin = false;
			}
			return true;

		}
		else
		{
			return false;
		}
	}

	public final boolean notExistUser(String name)
	{
		DataSet ds = DbHelperMySQL.Query("select * from customers_copy where username='" + name + "'");
		DataTable dt = ds.Tables[0]; //     Tables

		if (dt.Rows.size() == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}


	public final DataTable loadUsers()
	{
		DataSet ds = DbHelperMySQL.Query("select * from customers_copy  order by id desc");
		DataTable dt = ds.Tables[0]; //     Tables

		return dt;
	}
	//public bool a1; public bool a2;
	//public bool a3;
	//public bool b1; public bool b2;
	//public bool b3;

	public final void adduser(String name, String pwd)
	{


		String sql = "        insert into customers_copy(username,password,a1,a2,a3,b1,b2,b3) values('@name','@pwd','@a1','@a2','@a3','@b1','@b2','@b3')";
		sql = sql.replace("@name", name);
		sql = sql.replace("@pwd", pwd);
		sql = sql.replace("@a1", String.valueOf(a1));
		sql = sql.replace("@a2", String.valueOf(a2));
		sql = sql.replace("@a3", String.valueOf(a3));

		sql = sql.replace("@b1", String.valueOf(b1));
		sql = sql.replace("@b2",String.valueOf(b2));
		sql = sql.replace("@b3", String.valueOf(b3));

		DbHelperMySQL.ExecuteSql(sql);
	}
}